﻿using Neudesic.ComponentModel;
using System;
using System.ComponentModel;
using System.Reflection;

namespace Neudesic.Pulse.Entities
{
    /// <summary>
    /// Extensions class for Entities
    /// </summary>
    public static class EntityExtensions
    {

        /// <summary>
        /// Gets the search type for a given entity type.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <returns></returns>
        public static string GetSearchType(this EntityType entityType)
        {
            FieldInfo fi;
            SearchTypeAttribute attribute;

            fi = typeof(EntityType).GetField((entityType.ToString()));
            attribute = (SearchTypeAttribute)Attribute.GetCustomAttribute(fi, typeof(SearchTypeAttribute));

            if (attribute != null)
                return attribute.Description;

            return entityType.ToString();
        }

        /// <summary>
        /// Gets the search term for a given entity type.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <returns></returns>
        public static string GetSearchTerm(this EntityType entityType)
        {
            FieldInfo fi;
            SearchTermAttribute attribute;

            fi = typeof(EntityType).GetField((entityType.ToString()));
            attribute = (SearchTermAttribute)Attribute.GetCustomAttribute(fi, typeof(SearchTermAttribute));

            if (attribute != null)
                return attribute.Description;

            return entityType.ToString();
        }

        /// <summary>
        /// Gets the search term for a given entity type.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <returns></returns>
        public static string GetSearchResultNodeName(this EntityType entityType)
        {
            FieldInfo fi;
            SearchResultNodeAttribute attribute;

            fi = typeof(EntityType).GetField((entityType.ToString()));
            attribute = (SearchResultNodeAttribute)Attribute.GetCustomAttribute(fi, typeof(SearchResultNodeAttribute));

            if (attribute != null)
                return attribute.Description;

            return entityType.ToString();
        }

        /// <summary>
        /// Gets the search term for a given entity type.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <returns></returns>
        public static string GetDescription(this VoteRequestType entityType)
        {
            FieldInfo fi;
            DescriptionAttribute attribute;

            fi = typeof(VoteRequestType).GetField((entityType.ToString()));
            attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));

            if (attribute != null)
                return attribute.Description;

            return entityType.ToString();
        }
    }
}
